layui.use(['layer', 'element', 'laytpl', 'jquery', 'table', 'upload', 'form'], function() {
	//引入模块
	var layer = layui.layer,
		element = layui.element,
		laytpl = layui.laytpl,
		form = layui.form,
		upload = layui.upload,
		table = layui.table,
		$ = layui.jquery;

	if (localStorage.set != null) {
		//加载页面发生请求拿到数据
		$.ajax({
			url: "http://localhost:8080/sc/admin?type=home",
			async: true, //异步
			type: "POST",
			headers: {
				"token": localStorage.token
			},
			success: function(data) {
				//这里找id必须用js
				if (data.code == 'TokenFailed') {
					//删除token set
					delete localStorage.token;
					delete localStorage.set;
					layer.msg('登录失效，请重新登录！')
					setTimeout(function() {
						//调用父页面方法
						parent.tz();
					}, 1000);
				} else {
					var getTpl = document.getElementById('homemb').innerHTML,
						view = document.getElementById('homemoban');
					laytpl(getTpl).render(data, function(html) {
						view.innerHTML = html;
					});
				}
			}
		});

	} else {
		layer.msg('您还没有登录！')
		setTimeout(function() {
			parent.tz();
			//window.location.replace('admin_login.html');
		}, 1000);
	}

	//自动重载
	var token = localStorage.token;
	//表格重载
	table.reload('testReload', {
		url: 'http://localhost:8080/sc/admin4?type=getALLOrder',
		headers: {
			token: token
		} //设定异步数据接口的额外参数
	});

	//加载数据
	$.ajax({
		url: "http://localhost:8080/sc/admin4?type=getOrderJE",
		async: false, //异步
		type: "POST",
		headers: {
			"token": localStorage.token
		},
		success: function(data) {
			$('#zong').text(data.all);
			$('#jr').text(data.day);
		}
	});

	//头工具栏事件
	table.on('toolbar(tooltable)', function(obj) {
		switch (obj.event) {
			case 'allorder': //全部订单
				//自动重载
				var token = localStorage.token;
				//表格重载
				table.reload('testReload', {
					url: 'http://localhost:8080/sc/admin4?type=getALLOrder',
					headers: {
						token: token
					} //设定异步数据接口的额外参数
				});
				break;
			case 'moorder': //本月订单
				//自动重载
				var token = localStorage.token;
				//表格重载
				table.reload('testReload', {
					url: 'http://localhost:8080/sc/admin4?type=getMOOrder',
					headers: {
						token: token
					} //设定异步数据接口的额外参数
				});
				break;
			case 'dayorder': //当天订单
				//自动重载
				var token = localStorage.token;
				//表格重载
				table.reload('testReload', {
					url: 'http://localhost:8080/sc/admin4?type=getDayOrder',
					headers: {
						token: token
					} //设定异步数据接口的额外参数
				});
				break;
		};
	});

});
